package cn.net.shuxing.yifu.common.service;

import cn.net.shuxing.yifu.common.bean.User;
import cn.net.shuxing.yifu.common.bean.UserInfoBind;
import cn.net.shuxing.yifu.common.constant.UserType;
import cn.net.shuxing.yifu.common.mapper.UserInfoBindMapper;
import cn.net.shuxing.yifu.common.util.CommonExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

@Service
public class UserInfoBindService extends BaseService<UserInfoBind, UserInfoBindMapper> {

    @Autowired
    private UserService userService;

    @Autowired
    private UserInfoBindMapper userInfoBindMapper;

    //    public UserInfoBind getByPhoneNum(String phoneNum) {
//        UserInfoBind userInfoBind = new UserInfoBind();
//        userInfoBind.clearCreateTimeAndUpdateTime();
//        userInfoBind.setPhoneNum(phoneNum);
//        return get(userInfoBind);
//    }
//    public String getPhoneNumByUserId(Integer userId) {
//        UserInfoBind userInfoBind = new UserInfoBind();
//        userInfoBind.clearCreateTimeAndUpdateTime();
//        userInfoBind.setId(userId);
//        UserInfoBind userinfo = get(userInfoBind);
//        if (userinfo == null) {
//            return "";
//        } else {
//            return userinfo.getPhoneNum();
//        }
//    }

    public String getPhoneNumByUserId(Integer userId) {
        UserInfoBind userInfoBind = new UserInfoBind();
        userInfoBind.clearCreateTimeAndUpdateTime();
        userInfoBind.setId(userId);
        UserInfoBind userinfo = get(userInfoBind);
        String phoneNum = "";
        if (userinfo != null) {
            User user = userService.getByPrimaryKeyWithoutComplete(userId);
            Integer type = user.getType();
            if (type == UserType.MEMBER || type == UserType.NONE) {
                phoneNum =  userinfo.getPhoneNum();
            }
            if (type == UserType.SHAREHOLDER){
                phoneNum =  userinfo.getSPhoneNum();
            }
        }
        return phoneNum;
    }

    public UserInfoBind getByPhoneNum(String phoneNum) {
        CommonExample example = new CommonExample(UserInfoBind.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.orEqualTo("phoneNum", phoneNum);
        criteria.orEqualTo("sPhoneNum", phoneNum);
        example.and(criteria);
        UserInfoBind byExample = getByExample(example);
        return byExample;
    }

    public UserInfoBind getByMPhoneNum(String phoneNum) {
        CommonExample example = new CommonExample(UserInfoBind.class);
        Example.Criteria criteria = example.createCriteria();
        example.equals("phoneNum", phoneNum);
        return getByExample(example);
    }
    public UserInfoBind getBySPhoneNum(String phoneNum) {
        CommonExample example = new CommonExample(UserInfoBind.class);
        Example.Criteria criteria = example.createCriteria();
        example.equals("sPhoneNum", phoneNum);
        return getByExample(example);
    }


    public UserInfoBind getByMail(String mail) {
        UserInfoBind userInfoBind = new UserInfoBind();
        userInfoBind.clearCreateTimeAndUpdateTime();
        userInfoBind.setMail(mail);
        return get(userInfoBind);
    }

    public UserInfoBind checkUser(String username) {
        Example example = new Example(UserInfoBind.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.orEqualTo("phoneNum", username);
        criteria.orEqualTo("mail", username);
        criteria.orEqualTo("sPhoneNum", username);
        return userInfoBindMapper.selectOneByExample(example);
    }
}